﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Pointandclick;

namespace Pointandclick.ScreenManagement
{
    abstract class MenuScreen : BaseScreen
    {
        #region Properties

        SpriteFont font;
        List<MenuElement> options;

        /// <summary>
        /// Font used on the text options
        /// </summary>
        protected SpriteFont Font { get { return font; } set { font = value; } }

        /// <summary>
        /// List containing the options
        /// </summary>
        protected List<MenuElement> Options { get { return options; } set { options = value; } }

        #endregion

        #region Options Formatting

        /// <summary>
        /// Center the Elements on the frame with fixed spacing
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="spacing"></param>
        protected void CenterElements(Rectangle frame, int spacing)
        {
            int height = spacing * (options.Count() - 1);
            foreach (MenuElement option in options)
            {
                height += option.Rectangle.Height;
            }
            int yPos = frame.Y + (frame.Height - height) / 2;

            for (int i = 0; i < options.Count(); i++)
            {
                options[i].Rectangle = new Rectangle(frame.X+ (frame.Width - options[i].Rectangle.Width) / 2,
                                                        yPos,options[i].Rectangle.Width,options[i].Rectangle.Height);

                yPos += spacing + options[i].Rectangle.Height;
            }
        }

        /// <summary>
        /// Distribute Elements on the frame with even spacing
        /// </summary>
        /// <param name="frame"></param>
        protected void DistributeElements(Rectangle frame)
        {
            int height = 0;
            foreach (MenuElement option in options)
            {
                height += option.Rectangle.Height;
            }
            int spacing = (frame.Height - height)/(options.Count+1);
            int yPos = frame.Y + spacing;

            for (int i = 0; i < options.Count(); i++)
            {
                options[i].Rectangle = new Rectangle(frame.X + (frame.Width - options[i].Rectangle.Width) / 2,
                                                        yPos, options[i].Rectangle.Width, options[i].Rectangle.Height);

                yPos += spacing + options[i].Rectangle.Height;
            }
        }

        #endregion

        /// <summary>
        /// Draws the screen options
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="color">Color to tint the sprites. Use Color.White for no tinting</param>
        public override void Draw(SpriteBatch spriteBatch, Color color)
        {
            foreach (MenuElement option in options)
                option.Draw(spriteBatch, new Vector2(option.Rectangle.X,option.Rectangle.Y), color);
        }
    }
}
